<template>
	<view>
		<!-- 头部标题 -->
		<view class="cu-bar bg-white">
			<view class="action sub-title">
				<text class="text-xl text-bold text-green">作业上传</text>
				<text class="bg-green"></text>
				<!-- last-child选择器-->
			</view>
		</view>
		<!-- 头部标题 end -->

		<view class="cu-bar bg-white margin-top">
			<view class="action">
				图片上传
			</view>
			<view class="action">
				{{imgList.length}}/2
			</view>
		</view>

		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<2">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>

		<!-- 提交按钮 -->
		<view class="padding-xl">
			<button class="cu-btn block bg-gradual-green margin-tb-sm lg" @click="submit()">
				<text class="cuIcon-loading2 cuIconfont-spin"></text> 加载
			</button>
		</view>
		<!-- end -->


	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgList: [],
				str: [],
				workId: null
			}
		},
		onLoad(options) {
			this.workId = options.id
			console.log(this.workId)

		},
		methods: {

			submit() {
				// workId
				// uid
				// img
				var studentId = uni.getStorageSync('user_info').id
				var img =''
				this.str.forEach(function(e,index){
					if(index==0){
						img=e
					}else{
						img+=','+e
					}
				})
				console.log(img)
				var _this = this 
				// 发布
				uni.request({
					url: _this.api + "work/uploadWork",
					method: "POST",
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					data: {
						workId: this.workId,
						img: img,
						uid: studentId
					},
					success: (res) => {
						console.log(res)
						uni.showToast({
							title:res.data.message,
							icon: "none",
							duration: 1000
						})
					},
					fail: (err) => {
						uni.showToast({
							title: "提交失败！",
							icon: "none",
							duration: 1000
						})
					}
				})

			},

			ChooseImage() {
				uni.chooseImage({
					count: 2, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res.tempFiles[0].path)
						this.imgList.push(res.tempFiles[0].path)
						console.log(this.imgList)
						this.uploadImage()
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});


			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这个照片吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
							this.str.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			uploadImage() {
				var _this = this
				// 上传图片
				console.log(_this.imgList[_this.imgList.length - 1])
				uni.uploadFile({
					url: _this.api + "upload/postfile", //仅为示例，非真实的接口地址
					filePath: _this.imgList[_this.imgList.length - 1],
					name: 'file',
					success: (uploadFileRes) => {
						//数据上传
						console.log(uploadFileRes.data);
						_this.str.push(uploadFileRes.data)
						console.log(_this.str)

					},
					fail: (err) => {
						uni.showToast({
							title: "请稍后在试！图片上传失败！",
							duration: 1000,
							icon: "none"
						})
					}
				});
			}


		}
	}
</script>

<style>

</style>
